﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SIMS_GUI_Item
{
    public partial class CourseList : Form
    {
        public CourseList()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-3CMC9MB\SQLEXPRESS;Initial Catalog=SIMS GUI Item DataBase;Integrated Security=True;");
        private void CourseList_Load(object sender, EventArgs e)
        {
            this.gradeTableAdapter1.Fill(this.sIMS_GUI_Item_DataBaseDataSet9.Grade);
            var sql = "select -1 GradeId,'-全部-' GradeName union select* from dbo.Grade";
            SqlCommand comm = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            adp.Fill(ds);

            this.cbo_Grade.DisplayMember = "GradeName"; //设置那一列给用户看
            this.cbo_Grade.ValueMember = "GradeId";     //设置关键数据列
            this.cbo_Grade.DataSource = ds.Tables[0];   //设置Combobox的数据源

            //是否允许自动创建列
            this.dgv_CourseList.AutoGenerateColumns = false;
            //不允许出现空行
            this.dgv_CourseList.AllowUserToAddRows = false;
            //整行选中
            this.dgv_CourseList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.dgv_CourseList.MultiSelect = false;
            this.dgv_CourseList.RowTemplate.Height = 50;

        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select a.CourseName,b.GradeName from tab_Course a 
                        inner join Grade b on a.GradeId=b.GradeId where 1=1");
            if (Convert.ToInt32(this.cbo_Grade.SelectedValue) != -1)
                sb.AppendFormat(" and a.GradeId={0}", this.cbo_Grade.SelectedValue);
            SqlDataAdapter adp = new SqlDataAdapter(sb.ToString(), conn);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            this.dgv_CourseList.DataSource = ds.Tables[0];
        }
    }
}
